package cn.yd.rb.service.serviceimpl;

import cn.yd.rb.mapper.NewsMapper;
import cn.yd.rb.po.*;
import cn.yd.rb.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public List<News> findNewsAll() {
        NewsExample newsExample = new NewsExample();
        //newsExample.getOrderByClause();
        newsExample.setOrderByClause("id desc limit 6");
        List<News> newsList = newsMapper.selectByExample(newsExample);
        return newsList;
    }

    @Override
    public News findNewsById(Integer id) {
        News news = newsMapper.selectByPrimaryKey(id);
        return news;
    }

    @Override
    public PageBean<News> findNovelAll(Integer pc, Integer pageSiez, NewQueryVo newQueryVo) {
        PageBean<News> pb = new PageBean<News>();
        PageHelper.startPage(pc, pageSiez);//计算分页数据

        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        //添加条件
        newsExample.setOrderByClause("id desc");

        //存放条件
        String searchStr = "";

        //判断查询条件是否为null
        if(newQueryVo.getNews()!=null){
            //判断是否传入了名称
            if(newQueryVo.getNews().getTitle()!=null && !"".equals(newQueryVo.getNews().getTitle())){
                criteria.andTitleLike("%"+newQueryVo.getNews().getTitle()+"%");
                searchStr += "&news.title="+newQueryVo.getNews().getTitle();
            }
        }
        pb.setUrl(searchStr);

        List<News> list = newsMapper.selectByExample(newsExample);
        pb.setBeanList(list);//将当前页显示的记录存入集合

        PageInfo<News> info = new PageInfo<News>(list);
        pb.setInfo(info);

        return pb;
    }

    @Override
    public void addNews(News news) {
        newsMapper.insertSelective(news);
    }
}
